<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="dName">
        <el-input v-model="queryParams.dName" placeholder="请输入名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['zhongyao:dili:add']">手动新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-plus" size="mini" @click="selectAdd"
          v-hasPermi="['zhongyao:dili:add']">搜索新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['zhongyao:dili:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['zhongyao:dili:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['zhongyao:dili:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="diliList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="经度" align="center" prop="dJing" />
      <el-table-column label="纬度" align="center" prop="dWei" />
      <el-table-column label="名称" align="center" prop="dName" />

      <el-table-column label="图片" align="center" prop="picUrl">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="位置" align="center" prop="land" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['zhongyao:dili:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['zhongyao:dili:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改附近药店对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="经度" prop="dJing">
          <el-input v-model="form.dJing" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="dWei">
          <el-input v-model="form.dWei" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="名称" prop="dName">
          <el-input v-model="form.dName" placeholder="请输入名称" />
        </el-form-item>

        <el-form-item label="图片" prop="picUrl">
          <image-upload :limit="imgLimit" v-model="form.picUrl" />
        </el-form-item>
        <el-form-item label="位置" prop="land">
          <el-input v-model="form.land" placeholder="请输入位置" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <div>

    </div>

    <el-dialog :title="title" :visible.sync="open2" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="50px" :inline="true">
        <el-form-item label="名称" prop="dName">
          <el-input v-model="form.dName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item style="width: 400px;">
          <el-button type="primary" icon="el-icon-search" @click="jsondatas()"></el-button>
        </el-form-item>
        <el-form-item label="经度" prop="dJing">
          <el-input v-model="form.dJing" placeholder="请输入经度" disabled />
        </el-form-item>
        <el-form-item label="纬度" prop="dWei">
          <el-input v-model="form.dWei" placeholder="请输入纬度" disabled />
        </el-form-item>

        <el-form-item label="位置" prop="land">
          <el-input v-model="form.land" placeholder="请输入位置" disabled />
        </el-form-item>
        <el-form-item>
          <div class="drawStation">
            <div id="draw-station-container">
            </div>
          </div>
        </el-form-item>

        <el-form-item label="图片" prop="picUrl" style="margin-top: 20px;">
          <image-upload :limit="imgLimit" v-model="form.picUrl" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDili, getDili, delDili, addDili, updateDili, jsonDta } from "@/api/zhongyao/dili";
import AMapLoader from "@amap/amap-jsapi-loader"
export default {
  name: "Dili",
  data() {
    return {
      datalist: {},
      // 地图对象
      AMap: null,
      // 地图实例对象
      map: null,
      // 经纬度
      location: {
        lat: '',
        lng: ''
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 附近药店表格数据
      diliList: [], imgLimit: 1,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      name: '',
      open: false, open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dJing: null,
        dWei: null,
        dName: null,
        dAble: null,
        picUrl: null,
        land: null
      },
      centerList: [126.479248, 43.955898],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();

  },
  methods: {
    initMap() {
      AMapLoader.load({
        key: 'b7a6bfac66ebb6f770e1fb127fd0c892', // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: ['AMap.Scale', 'AMap.PlaceSearch', 'AMap.AutoComplete'], // 需要使用的的插件列表，如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          this.AMap = AMap
          this.map = new AMap.Map("draw-station-container", {
            //设置地图容器id
            viewMode: "3D", //是否为3D地图模式
            zoom: 15, //初始化地图级别
            center: this.centerList, //初始化地图中心点位置
            // mapStyle: 'amap://styles/macaron', //设置地图的显示样式
          })
          // 地图点击事件
          this.map.on('click', this.clickMapHandler)
        })
        .catch((e) => {
          console.log(e)
        })
    },
    // 点击地图事件
    clickMapHandler(e) {

      this.form.dJing = e.lnglat.getLng()
      this.form.dWei = e.lnglat.getLat()
    },
    jsondatas() {

      jsonDta(this.form.dName).then(response => {
       
        if (response.length === 0) {
          this.$message("搜索不存在");
        } else {
          this.datalist = response[0]
          console.log( this.datalist )
          this.form.dName = response[1].title
          this.form.land = this.datalist.address
          this.form.dJing = this.datalist.location.lng
          this.form.dWei = this.datalist.location.lat
          this.centerList[0] = this.datalist.location.lng
          this.centerList[1] = this.datalist.location.lat
         
          this.initMap()
        }

      })
    },

    /** 查询附近药店列表 */
    getList() {
      this.loading = true;
      listDili(this.queryParams).then(response => {
        this.diliList = response.rows;
        // console.log(this.diliList)
        this.diliList = this.diliList.filter((item) => {
          return item.dName !== "我的位置"
        })

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dKey: null,
        dJing: null,
        dWei: null,
        dName: null,
        dAble: null,
        picUrl: null,
        land: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dKey)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加附近药店";
    },
    selectAdd() {
      this.initMap()
      //DOM初始化完成进行地图初始化
      this.reset();
      this.open2 = true;
      this.title = "添加附近药店";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dKey = row.dKey || this.ids
      getDili(dKey).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改附近药店";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dKey != null) {
            updateDili(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDili(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.open2=false
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dKeys = row.dKey || this.ids;
      this.$modal.confirm('是否确认删除附近药店编号为"' + dKeys + '"的数据项？').then(function () {
        return delDili(dKeys);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zhongyao/dili/export', {
        ...this.queryParams
      }, `dili_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.drawStation #draw-station-container {
  padding: 0px;

  /* margin: 20px 0 20px 0; */
  margin: 20px auto;
  width: 500px;
  height: 300px;
}

.el-input {}
</style>